<template>
  <div class="allocate">
    <div class="title">{{ $t("dialog.task_to_order.title") }}</div>
    <div class="task-box">
      <div class="task-allocate" v-if="!isAllocated">
        <div class="task-id">
          <span class="label">{{ $t("table.profile_task.col_id") }}:</span
          ><span class="value">{{ taskDetailData.id }}</span>
        </div>
        <div class="user-select">
          <span class="label">{{
            $t("dialog.task_to_order.label_allocate_to")
          }}</span>
          <div class="control">
            <a-select v-model:value="selectValue">
              <a-select-option
                v-for="item in receiverData"
                :key="item.id"
                :value="item.id"
                >{{
                  item.member_last_name +' '+ item.member_first_name
                }}</a-select-option
              >
            </a-select>
          </div>
        </div>
      </div>
      <!-- 分配成功 -->
      <div class="aollcate-success" v-else>
        <div
          v-html="
            $t('dialog.task_to_order.complete_descri').replace(
              '%',
              taskDetailData.id
            )
          "
        ></div>
      </div>
      <div class="footer_btns">
        <div class="inner">
          <a-button
            @click="goBack"
            :style="{
              background: isAllocated ? 'rgb(19, 194, 156)' : '',
              color: isAllocated ? '#fff' : '',
            }"
            >{{
              isAllocated ? $t("btnText.close") : $t("btnText.cancel")
            }}</a-button
          >
          <a-button
            v-if="!isAllocated"
            type="primary"
            @click="allocateToUser"
            style="background: rgb(19, 194, 156)"
            >{{ $t("btnText.confirm_allocate") }}</a-button
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { message, Modal } from "ant-design-vue";
import { getCurrentInstance, h, onBeforeMount, onMounted, ref } from "vue";
import { postTaskOrderList, postTaskOrderToUser } from "../../api/task";

const $t = getCurrentInstance()?.appContext.config.globalProperties.$t;

const goBack = () => {
  goto(-1);
};

const isAllocated = ref(false);

const taskDetailData = ref();

const receiverData = ref();
const getReceiverDataList = async () => {
  let res = await postTaskOrderList(taskDetailData.value.id);
  if (res.code == 200) {
    receiverData.value = res.data || [];
  }
};

onBeforeMount(() => {
  let session_data = sessionStorage.getItem("taskDetailData");
  if (session_data) {
    taskDetailData.value = JSON.parse(session_data);
  } else {
    goBack();
  }
  getReceiverDataList();
});

const selectValue = ref("");
const submiting = ref(false);
const allocateToUser = () => {
  if (selectValue.value == "")
    return message.warning($t("message.task_no_allocate_user"));
  let receiver = receiverData.value.find(
    (item) => item.id == selectValue.value
  );
  let receiver_name = receiver.member_last_name +' ' + receiver.member_first_name;
  let confirmModal = Modal.confirm({
    title: $t("dialog.task_to_order.title"),
    content: $t("dialog.task_to_order.modal_desc").replace("%", receiver_name),
    icon: () => h("", ""),
    centered: true,
    async onOk() {
      submiting.value = true;
      confirmModal.update({
        okButtonProps: {
          loading: submiting.value,
        },
      });
      let params = {
        task_id: taskDetailData.value.id,
        member_id: selectValue.value,
      };
      let res = await postTaskOrderToUser(params);
      if (res.code === 200) {
        submiting.value = false;
        message.success($t("message.success.allocate"));
        isAllocated.value = true;
      } else {
        message.success($t("message.error.allocate"));
        submiting.value = false;
      }
      confirmModal.update({
        okButtonProps: {
          loading: submiting.value,
        },
      });
    },
    onCancel() {
    },
    class: "profile-confirm-modal",
    okText: $t("btnText.ensure"),
    cancelText: $t("btnText.cancel1"),
    width: "19.7917vw",
    maskClosable: true,
  });
};
</script>
<style scoped lang="less">
.allocate {
  padding: 82px;
  .title {
    font-size: 82px;
  }
}
.task-box {
  font-size: 72px;
  min-height: 400px;
  .aollcate-success {
    font-size: 72px;
    text-align: center;
    height: 800px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    & > div {
      display: flex;
      flex-direction: column;
      gap: 42px;
    }
  }
}
.task-allocate {
  padding-top: 82px;
  & > div {
    gap: 20px;
  }
  .task-id {
    margin-bottom: 82px;
    .value {
      display: inline-block;
      margin-left: 42px;
    }
  }
  .user-select {
    align-items: flex-start;
    flex-direction: column;
    & > * {
      flex: 1;
    }
    :deep(.ant-select) {
      margin-top: 42px;
      width: 100%;
    }
  }
}
.footer_btns {
  .inner {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    padding: 82px;
    border-top: 1px solid #eee;
    background-color: #fff;
    gap: 82px;
    & > * {
      padding: 0!important;
      flex: 1;
    }
  }
}
</style>
